﻿using System;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
/// <summary>
/// Summary description for AutoComplete
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
// [System.Web.Script.Services.ScriptService]
public class AutoComplete : System.Web.Services.WebService {

    public AutoComplete () {

        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }

    [WebMethod]
    public string[] GetCountriesList(string prefixText)
    {
        DataSet dtst = new DataSet();
        SqlConnection sqlCon = new SqlConnection(ConfigurationManager.AppSettings["ConnectionString"]);
        string strSql = "SELECT  top  10   UD_FirstName FROM         UserDetails where UD_FirstName " +
                        "like '" + prefixText + "%'";
        SqlCommand sqlComd = new SqlCommand(strSql, sqlCon);
        sqlCon.Open();
        SqlDataAdapter sqlAdpt = new SqlDataAdapter();
        sqlAdpt.SelectCommand = sqlComd;
        sqlAdpt.Fill(dtst);
        string[] cntName = new string[dtst.Tables[0].Rows.Count];
        int i = 0;
        try
        {
            foreach (DataRow rdr in dtst.Tables[0].Rows)
            {
                cntName.SetValue(rdr["UD_FirstName"].ToString(), i);
                i++;
            }
        }
        catch { }
        finally
        {
            sqlCon.Close();
        }
        return cntName;
    } 
    //public static string[] GetCompletionList(string prefixText, int count, string contextKey)
    //{

    //    Purchase.Purchase purchase = new Purchase.Purchase();
    //    DataSet userDs = purchase.AutoCompleteUserName(prefixText, count);
    //    //string connectionString = ConfigurationManager.ConnectionStrings["TestConnection"].ConnectionString;
    //    //SqlConnection conn = new SqlConnection(connectionString);
    //    // Try to use parameterized inline query/sp to protect sql injection
    //    //  SqlCommand cmd = new SqlCommand("SELECT TOP " + count + " Name FROM tblAgent WHERE Name LIKE '" + prefixText + "%'", conn);
    //    // SqlDataReader oReader;
    //    // conn.Open();
    //    List<String> CompletionSet = new List<string>();
    //    if (userDs.Tables[0].Rows.Count > 0)
    //    {
    //        for (int userCount = 0; userCount < userDs.Tables[0].Rows.Count; userCount++)
    //        {
    //            CompletionSet.Add(userDs.Tables[0].Rows[userCount]["UD_FirstName"].ToString());
    //        }
    //    }
    //    return CompletionSet.ToArray();
    //}
    
}

